import config from './config.js';
uni.setStorageSync("token", `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzEsImV4cCI6MTcwOTcwNDA0MywidXNlcl9uYW1lIjoi6ICB5byvIiwianRpIjoiNzdkMDljOWYtY2U5YS00OGZmLWIyZjktZmMxNWQyNTk0YmFiIiwiY2xpZW50X2lkIjoiY2xpZW50LWFwcCIsInNjb3BlIjpbImFsbCJdfQ.XJXvp2N9EcwybNmkBBJXNqey98vB-RzWP8-gmMRH2bKlw9PsComsvh5QOo7Rxk5sD3IlgIMmdFN3dPah9sHCZoSkeRtVm53wbcopoz2L9zUxxSz9LB1O0xpPJimcc6Smn4yo_EITyus6NMP8yNCrVj9IsSlnHPf1sWviBy93TtqO05oryUw1XqNw-zCqKH3u0vuvfpFeavcMnG9VI2yh4tQ-6gZk7IVCTOHoPNDZ7ACD-1YtBpB0oJCtvukxXBWlHsKFs0_yXVo8Q8btWjy0eI8iMOgmRnfy8pf5D5qY84hwYVikVlQPU5dGvQ4BtsZL9HkOYI1Nqncu0pWTmWCF5w`)
export default {
	request (options = {}) {
		return new Promise((resolve, reject) => {
			const token = uni.getStorageSync("token")
			options.url = config.baseUrl + options.url
			options.header.Authorization = 'Bearer ' + token;
			options.complete = (response) => {
				if (response.statusCode == 200 || response.statusCode == 0) {
					if (response.data.code == 401) {
						uni.navigateTo({
							url: "/pages/user/login"
						})
					}

					if (response.data.code == 500) {
						uni.showToast({
							title: response.data.msg,
							icon: "none",
							duration: 2000
						});
					}
					resolve(response.data)
				} else {
					uni.showToast({
						title: '请求异常',
						icon: "none"
					});
				}
			}

			uni.request(options)
		})
	},

	post (url, data = {}, header = {}) {

		let options = {
			url: url,
			data: data,
			header,
			// header: { ...header, 'Content-Type': 'application/x-www-form-urlencoded' },
			method: "POST"
		}

		return this.request(options);
	},

	get (url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}

		return this.request(options);
	}
};